<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

session_start();
$objMemberInfo = $_SESSION["Member_Info"];
if(time() - $objMemberInfo -> WagerCommonTime <= 1){
	sleep(1);
}
$objMemberInfo -> WagerCommonTime = time();

$_LangPath = "wagercommon";	//語言檔參數，務必放在最前面
include_once(__Member_Path."/logincheck.php");
include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$arrLotto = $_arrLotto[$intLID];
$strSType = $_data["stype"];
$strChoiceNumber = "";
$intOddsID = 0;
$flOdds = 0;

$arrNum = array();
$arrStake = array();
$arrBT = array();
$arrELimit = array();
$arrBLimit = array();
$arrMin = array();
$arrError = array();
$intTotalWGold = 0;
$arrChoiceNumber = explode(",", $_data["ChoiceNumber"]);
if(sizeof($arrChoiceNumber) > 60){
	$Err_Message = "Data Over 60 Items!!";
	include("wager_error.php");
}

foreach($arrChoiceNumber as $strCNum){
	$arrCNum = explode("@", $strCNum);
	$arrCNum[0] = trim($arrCNum[0]);
	$arrCNum[1] = trim($arrCNum[1]);
	$intStake = (int)$arrCNum[1];
	if($intStake <= 0 || !preg_match("/^[0-9]+$/", $arrCNum[1])){
		$strError = str_replace("{Number}", $arrCNum[0], $_strWagerCommon["GoldErr"]);
		$strError = str_replace("{Gold}", $arrCNum[1], $strError);
		$arrError[] = $strError;
		continue;
	}
	switch(true){
		case preg_match("/[0-9]{2}[\*x]{2}/", $arrCNum[0]):
			$intBT = 1;
			break;

		case preg_match("/[0-9]{1}[\*x][0-9]{1}[\*x]/", $arrCNum[0]):
			$intBT = 2;
			break;

		case preg_match("/[0-9]{1}[\*x][\*x][0-9]{1}/", $arrCNum[0]):
			$intBT = 3;
			break;

		case preg_match("/[\*x][0-9]{2}[\*x]/", $arrCNum[0]):
			$intBT = 4;
			break;

		case preg_match("/[\*x][0-9]{1}[\*x][0-9]{1}/", $arrCNum[0]):
			$intBT = 5;
			break;

		case preg_match("/[\*x]{2}[0-9]{2}/", $arrCNum[0]):
			$intBT = 6;
			break;

		case preg_match("/[0-9]{3}[\*x]/", $arrCNum[0]):
			$intBT = 7;
			break;

		case preg_match("/[0-9]{2}[\*x][0-9]/", $arrCNum[0]):
			$intBT = 8;
			break;

		case preg_match("/[0-9]{1}[\*x][0-9]{2}/", $arrCNum[0]):
			$intBT = 9;
			break;

		case preg_match("/[\*x][0-9]{3}/", $arrCNum[0]):
			$intBT = 10;
			break;

		case preg_match("/^([0-9]){4}$/", $arrCNum[0]):
			$intBT = 11;
			break;

		case preg_match("/^([0-9]){2}$/", $arrCNum[0]):
			$intBT = 12;
			break;

		case preg_match("/^([0-9]){3}$/", $arrCNum[0]):
			$intBT = 13;
			break;

		default:
			$arrError[] = str_replace("{Number}", $arrCNum[0], $_strWagerCommon["NumberErr"]);
			continue;
			break;
	}
	$strNum = $arrCNum[0];
	$strNum = str_replace("x", "", $strNum);
	$strNum = str_replace("*", "", $strNum);
	$arrNum[$arrCNum[0]] = (int)$strNum;
	$arrStake[$arrCNum[0]] += $intStake;
	$arrBT[$arrCNum[0]] = $intBT;
	//單注限額
	if($arrBLimit[$intBT] < $arrStake[$arrCNum[0]])$arrBLimit[$intBT] = $arrStake[$arrCNum[0]];
	//單場限額
	$arrELimit[$intBT][(int)$strNum] += $intStake;
	//注金總額
	$intTotalWGold += $intStake;
	//最低下注金額
	//$arrMin[$intBT] = $arrStake[$arrCNum[0]];
}
if(sizeof($arrError) > 0){
	$Err_Message = implode("<br />\n", $arrError);
	include("wager_error.php");
}

if(sizeof($arrNum) == 0){
	$Err_Message = "No Wager Number Data!!";
	include("wager_error.php");
}
//ksort($arrNum);

include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");
include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/RealBet.lib.php");
include_once(__Common_Path."/gambletype/GambleType".$objMemberInfo -> GambleType."_".$intLID.".inc.php");//加進分盤資料
include_once(__Common_Path."/CommToOddsRate_".$intLID.".inc.php");
$arrUserOPlus = $objMemberInfo -> UserCommToOdds[$intLID];

if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}
$intAN = 0;

include_once(__Root_Path."/tmp/credit-".$objMemberInfo -> ID.".php");
//檢查信用額度是否超過
if($objMemberInfo -> CreditLine < $intTotalWGold + $_arrMemStake["_SUM"]){
	$Err_Message = $_strWagerCommon["overCredit"].($objMemberInfo -> CreditLine - $_arrMemStake["_SUM"]);
	include("wager_error.php");
}

//檢查單場單注
foreach($arrELimit as $intBT => $arrEL){
	if(!is_array($_arrBetParam[$intBT])){
		$Err_Message = "No Wager Relation Data!!";
		include("wager_error.php");
	}

	//檢查公司預收單注限額
	$chkCompBetLimit = floor($_arrBetParam[$intBT][$intAN]["Com_BetLimit"] / ($objMemberInfo -> Layer6Scale*$flMemERate));

	//檢查單注限額
	$chkBetLimit = $objMemberInfo -> arrBetLimit[$intLID][$intBT];
	$_arrLottoLimit[$intBT][1] = floor($_arrLottoLimit[$intBT][1] / $flMemERate);
	if($chkBetLimit > $_arrLottoLimit[$intBT][1]){
		$chkBetLimit = $_arrLottoLimit[$intBT][1];
	}
	if($chkBetLimit > $chkCompBetLimit){
		$chkBetLimit = $chkCompBetLimit;
	}
	if($arrBLimit[$intBT] > $chkBetLimit){
		$Err_Message = $_arrBet_Type[$intLType][$intBT].$_strWagerCommon["overBetLimit2"].$chkBetLimit;
		include("wager_error.php");
	}

	//檢查單場限額
	$chkEventLimit = $objMemberInfo -> arrEventLimit[$intLID][$intBT];
	$_arrLottoLimit[$intBT][0] = floor($_arrLottoLimit[$intBT][0] / $flMemERate);
	if($chkEventLimit > $_arrLottoLimit[$intBT][0]){
		$chkEventLimit = $_arrLottoLimit[$intBT][0];
	}
	foreach($arrEL as $intNum => $intELimit){
		if($intELimit + $_arrMemStake[$intBT][$intGID][$intNum] > $chkEventLimit){
			$Err_Message = $_arrBet_Type[$intLType][$intBT].$_strWagerCommon["overEventLimit"].$chkEventLimit;
			include("wager_error.php");
		}
	}
}

$arrWagerCon = array();
$intTotalWGold = 0;
$arrELimit = array();
$arrBLimit = array();

//取得最新號碼賠率資料
foreach($arrNum as $strK => $intNum){
	$intBT = $arrBT[$strK];
	$intStake = $arrStake[$strK];

	$intAN = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
	if($intAN == null)$intAN = 0;

	//檢查最低下注金額
	$chkMinGold = floor($_arrBetParam[$intBT][$intAN]["MinStake"] / $flMemERate);

	if($intStake < $chkMinGold){
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_arrBet_Type[$intLType][$intBT].$_strWagerCommon["underMinStake2"].$chkMinGold."<br />".$strBTTip;
		include("wager_error.php");
	}

	//退水轉賠率
	$flOddsPlus = $arrUserOPlus[$intBT][$intAN][11] * $_arrChRate[$intBT][$intAN][$objMemberInfo -> GambleType + 10];

	$arrGameDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, $intNum, $intOddsID, $flOdds, "");
	if($arrGameDate["isError"]){
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_strWagerCommon[$arrGameDate["Err_Message"]]."<br />".$strBTTip;
		include("wager_error.php");
	}
	$flOdds = floatval($arrGameDate["Odds"]) + $_arrGambleType[$intBT][$intAN] + $flOddsPlus;
	$arrWagerCon[$strK] = getWagerConditions($intLType, $intBT, $intNum, $flOdds, $arrGameDate["SerialNo"]);
	$arrWagerCon[$strK] .= "<br><b style='color: #0000FF'>$".$intStake."</b>";
	$arrWagerCon[$strK] = preg_replace("/.+<br \/>/", "", $arrWagerCon[$strK]);
	$intTotalWGold += $intStake;

	$arrFrmWagerCon[$strK] = $intNum."#".$arrGameDate["OddsID"]."#".$intStake."#".$intBT;

	//檢查是否大於最高派彩
	$intChkMaxPayOff = floor($_arrBetParam[$intBT][$intAN]["MaxPayOff"] / $flMemERate);
	if(($flOdds - 1)*$intStake > $intChkMaxPayOff){
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_strWagerCommon["Number"]." ".$strK." ".$_strWagerCommon["overMaxPayOff2"].$intChkMaxPayOff."<br />".$strBTTip;
		include("wager_error.php");
	}
}

$strWagerConditions = join("<br />", $arrWagerCon);
foreach($arrBT as $intBT){
	$strWagerConditions = WagerFormTrans($intLID, $intBT, $strWagerConditions);
}

$fp = @file(__Membertemplate_Path."/7star_qwager.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);
$fp = str_replace("{sid}", $_data["sid"], $fp);
$fp = str_replace("{ShowAccount}", $objMemberInfo -> Account, $fp);
$fp = str_replace("{ShowCredit}", $objMemberInfo -> CreditLine, $fp);
$fp = str_replace("{LottoName}", $arrLotto["Lotto_".$_intLang], $fp);
$fp = str_replace("{GambleIDValue}", $intGID, $fp);
$fp = str_replace("{LottoIDValue}", $intLID, $fp);
$fp = str_replace("{UserIDValue}", $objMemberInfo -> ID, $fp);
$fp = str_replace("{WagerConditions}", $strWagerConditions, $fp);
$fp = str_replace("{ShowWagerContent}", join("@", $arrFrmWagerCon), $fp);
$fp = str_replace("{UseCreditValue}", $_arrMemStake["_SUM"], $fp);	//已使用信用額度
$fp = str_replace("{ShowStakeMoney}", $intTotalWGold, $fp);	//下注總金額
$fp = str_replace("{ShowStakeNo}", sizeof($arrNum), $fp);	//下注號碼數

$arrCheckKey = split(" ", microtime());
$fp = str_replace("{CheckKeyValue1}", $arrCheckKey[0], $fp);
$fp = str_replace("{CheckKeyValue2}", $arrCheckKey[1], $fp);

while(list($k, $v) = each($_strWagerCommon)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

//輸出
WebOutputHTML($fp, 1);
?>